package practice1_100;

import java.util.*;

public class Practice10_02 {
    public List<List<String>> groupAnagrams(String[] strs) {
        //用map存储字符串中 内容相同顺序不同的str
        HashMap<String,List<String>> map=new HashMap<>();
        for(String str:strs){
            char[] chars=str.toCharArray();
            int[] count=new int[26];//记录字符串a-z有多少个
            for (char c:chars){
                count[c-'a']++;
            }
            StringBuffer stringBuffer=new StringBuffer();
            for (int i = 0; i <26 ; i++) {
                stringBuffer.append((char)('a'+i));
                stringBuffer.append(count[i]);
            }
            List<String> list =map.getOrDefault(stringBuffer.toString(),new ArrayList<String>());
            list.add(str);
            map.put(stringBuffer.toString(),list);
        }
        for (String str:map.keySet()){
            Collections.sort(map.get(str));
        }
        return  new ArrayList<List<String>>(map.values());
    }

    public static void main(String[] args) {
        List<List<String>> list=new Practice10_02().groupAnagrams(new String[]{"eat","tea","tan","ate","nat","bat"});
        System.out.println(list);
    }
}
